

package com.yonyong.tao.commons.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

/**
 * @Describtion 操作记录
 * @Author yonyong
 * @Date 2019/10/30 15:37
 * @Version 1.0.0
 **/
@Aspect
@Component
public class SystemLogAop {

    @Pointcut("@annotation(com.yonyong.tao.commons.aop.Log)")
    public void pointcut(){}

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        try{
            Object result = joinPoint.proceed();
            //执行时常(毫秒)
            long time = System.currentTimeMillis()-beginTime;
            //保存日志
            savaLog(joinPoint,time,"成功");
            return result;
        }catch (Exception e){
            //执行时常(毫秒)
            long time = System.currentTimeMillis()-beginTime;
            //保存日志
            JSONObject jsonObject = JSON.parseObject(JSONObject.toJSONString(e));
            jsonObject.remove("stackTrace");
            savaLog(joinPoint,time,"失败");
            throw e;
        }
    }

    private void savaLog(ProceedingJoinPoint joinPoint, long time,String result) throws UnknownHostException {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
//        ShopLog shopLog = new ShopLog();
        Log log = method.getAnnotation(Log.class);
        if (log != null){
            //注解上的描述
//            shopLog.setOperation(log.value());
//            shopLog.setComment(log.comment());
        }
        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
//        shopLog.setMethod(className+"."+methodName+"()");
        //请求的参数
       try {
           Object[] args = joinPoint.getArgs();
           String paramString = null;
           String params = "";
           for (int i=0; i< args.length;i++){
               if (isPrimitive(args[i])){
                   paramString = args[i].toString();
               }else {
                   paramString = args[i].toString();
               }
               if (null != paramString && paramString.length()>4999){
                   paramString = paramString.substring(0,4999);
               }
               params = params + "参数"+String.valueOf(i+1)+":"+ JSONObject.toJSONString(paramString) + ";" ;
           }

//           shopLog.setParams(params);
       }catch (Exception e){
//           shopLog.setParams(null);
       }

        //获取request
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //设置ip地址
        InetAddress ip4 = InetAddress.getLocalHost();
        String ip = ip4.getHostAddress();
    }

    /**判断一个对象是否是基本类型或基本类型的封装类型*/
    private boolean isPrimitive(Object obj) {
        try {
            return ((Class<?>)obj.getClass().getField("TYPE").get(null)).isPrimitive();
        } catch (Exception e) {
            return false;
        }
    }
}